﻿function AddTask() {
    var form = $('#task-form');
    $.ajax({
        type: 'POST',
        dataType: 'HTML',
        url: 'Task/AddTask',
        data: form.serialize(),
        success: function (data) {
            AppendTask(data, "add");
        }
    });
}

function SaveTask() {
    var form = $('.task-save').parent().next();

    $.ajax({
        async: false,
        type: 'POST',
        dataType: 'HTML',
        url: 'Task/EditTask',
        data: form.serialize(),
        success: function (data) {
            AppendTask(data, "save");
        },
    });
}

function AppendTask(data, action){
    var project = $('#ProjectId :selected').text();
    if (project == 'Choose...') {
        project = 'Inbox';
    }    
    var current = $('.main-nav-items .selected').attr('name');
    var taskid = $(data).attr('id');


    // add new task
    if(action == "add"){

        // if selected project list
        var selectingProject = $('.project-list-items .selected').attr('name');
        if(selectingProject == project){
            $('.task-list').first().append(data);
        }

        if (current == 'inbox') {
            if(project == 'Inbox'){
                $('.task-list').first().append(data);
            }
        }
        if (current == 'upcoming') {
        }
        if (current == 'all') {
            if(!$('div[name="' + project + '"]').is(":visible")){
                $('div[name="' + project + '"]').show();
            }

            $('div[name="' + project + '"]').next().append(data);
        } 
    }
    // save task
    if(action == "save"){

        // if selected project list
        var selectingProject = $('.project-list-items .selected').attr('name');

        if(selectingProject == project){
            $('#task-id-' + taskid).replaceWith(data);
        }else{
            if(selectingProject != null){
                $('#' + taskid).remove();
            }
        }

        if (current == 'inbox' || current == null) {
            $('#' + taskid).replaceWith(data);
        }
        if (current == 'upcoming') {
        }
        if (current == 'all') {
            var currentProject = $('#task-id-' + taskid).parent().prev().attr('name');
            if(currentProject == project){
                $('#' + taskid).replaceWith(data);
            } else{
                $('#' + taskid).remove();
                if(!$('div[name="' + project + '"]').is(":visible")){
                    $('div[name="' + project + '"]').show();
                }
                $('div[name="' + project + '"]').next().append(data);                
            }
        }         

    }

    $('#mask, .window').hide();
    $('.add-task-btn').toggleClass('pressed');
    $('#task-form')[0].reset();
    $('.add-task-btn').removeClass('pressed');
    UpdateTaskCount();
    clearFormContainer();  
    RemoveEmptyTab();
    GetScrollBar();
}

function GetTaskDetails(taskid){
    $.ajax({
        async: false,
        type: 'POST',
        dataType: 'HTML',
        url: 'Task/ShowTask',
        data: {id:taskid},
        success: function (data) {
            $('#task-details-content').empty();
            $('#task-details-content').html(data);
        }        
    });
}

function GetTaskForm() {
    if($('#task-form').length == 0)
    {
        var projectId = $('.project-list-items li.selected').attr('class');
        if (projectId != null) {
            projectId = projectId.split(' ')[0].split('-')[1];
        }

        var id = $(this).attr('href');
        $.ajax({            
            type: 'GET',
            dataType: 'HTML',
            data: { projectId: projectId },
            url: 'Task/AddTask',
            success: function (data) {
                $('#task-form-conteiner').append(data);
                formShow($('#task-form-conteiner'));
                $('#Name').focus();
                $('.add-task-btn').addClass('pressed');
            }
        });
    }
}

function AddProject(form){
    $.ajax({
        type: 'POST',
        dataType: 'HTML',
        url: 'Project/AddProject',
        data: form.serialize(),
        success: function (data) {
            $('.project-list-items').append(data);
            tasklistname = ($(data).children().next().html());
            $('.content').append('<div class="task-tab" style="display:none" name="'+tasklistname+'">'+tasklistname+'</div><ul class="task-list ui-sortable"></ul>');
            $('#mask, .window').hide();
            clearFormContainer();
        }
    });    
}

function SaveProject(listid, form){
    $.ajax({
        type: 'POST',
        dataType: 'HTML',
        url: 'Project/EditProject',
        data: form.serialize(),
        success: function (data) {
            $('.project-' + listid).replaceWith(data);
            $('#mask, .window').hide();
            clearFormContainer();            
        }

    });
}